import { defineStore } from 'pinia'

export const useSettingsStore = defineStore('settings', {
  state: () => ({
    stage: location.href.toLocaleLowerCase().includes('xxzhpj') ||
      location.href.toLocaleLowerCase().includes('xxxsgl') ||
      location.href.toLocaleLowerCase().includes('xxtcgl') ||
      location.href.toLocaleLowerCase().includes('xxyxsj')
        ? '1'
        : location.href.toLocaleLowerCase().includes('czzhpj') ||
          location.href.toLocaleLowerCase().includes('czxsgl') ||
          location.href.toLocaleLowerCase().includes('cztcgl') ||
          location.href.toLocaleLowerCase().includes('czyxsj') ||
          location.href.toLocaleLowerCase().includes('czyjfw')
        ? '2'
        : location.href.toLocaleLowerCase().includes('gzzhpj') ||
          location.href.toLocaleLowerCase().includes('gzxsgl') ||
          location.href.toLocaleLowerCase().includes('gztcgl') ||
          location.href.toLocaleLowerCase().includes('gzyjfw')
        ? '3'
        : '2',
    // 添加当前可操作的学期ID
    currentOperableSemesterId: '',
    // 可操作学期列表（支持多个学期可操作的扩展场景）
    operableSemesterIds: []
  }),
  
  actions: {
    // 直接修改设置项
    changeSetting(data) {
      const { key, value } = data
      if (this.hasOwnProperty(key)) {
        this[key] = value
      }
    },
    // 设置当前可操作的学期ID
    setCurrentOperableSemester(semesterId) {
      this.currentOperableSemesterId = semesterId
    },
    // 设置可操作学期列表
    setOperableSemesters(semesterIds) {
      this.operableSemesterIds = semesterIds
    }
  },
  
  getters: {
    // 判断指定学期是否可操作
    isSemesterOperable: (state) => (semesterId) => {
      // 检查是否在可操作学期列表中，或是否是当前可操作学期
      return state.operableSemesterIds.includes(semesterId) || state.currentOperableSemesterId === semesterId
    }
  }
})